/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.events;

import com.mobiwebinc.compconn.configuration.ServerConfiguration;
import com.mobiwebinc.compconn.events.command.Command;
import com.mobiwebinc.compconn.events.command.Decoder;
import java.awt.event.KeyEvent;

/**
 *
 * @author suraj
 */
public class AppSwitcher implements EventHandler {

    ServerConfiguration svr;

    public AppSwitcher() {
        svr = ServerConfiguration.getInstance();
    }

    public void handleRequest(String cmd) {
        if (cmd.startsWith(Command.APP_SWITCH_START)) {
            system.keyPress(svr.appSwitchKey());
        } else if (cmd.startsWith(Command.APP_SWITCH_NOW)) {
            processAppSwitch(cmd);
        } else if (cmd.startsWith(Command.APP_SWITCH_END)) {
            system.keyRelease(svr.appSwitchKey());
        } else if (cmd.startsWith(Command.APP_WINDOW_SWITCH_START)) {
            system.keyPress(svr.appSwitchKey());
        } else if (cmd.startsWith(Command.APP_WINDOW_SWITCH_END)) {
            system.keyRelease(svr.appSwitchKey());
        } else if (cmd.startsWith(Command.APP_WINDOW_SWITCH)) {
            processWinSwitch(cmd);
        }
    }

    public boolean isSupported(String cmd) {
        return cmd.startsWith(Command.APP_PREFIX);
    }

    private void processAppSwitch(String cmd) {
        int[] params = Decoder.intParameters(cmd);
        if (params[0] == 0) {
            system.keyPress(KeyEvent.VK_SHIFT);
        }
        system.keyPress(KeyEvent.VK_TAB);
        system.keyRelease(KeyEvent.VK_TAB);
        if (params[0] == 0) {
            system.keyRelease(KeyEvent.VK_SHIFT);
        }
    }

    private void processWinSwitch(String cmd) {
        int[] params = Decoder.intParameters(cmd);
        if (params[0] == 0) {
            system.keyPress(KeyEvent.VK_SHIFT);
        }
        system.keyPress(svr.windowSwitcher());
        system.keyRelease(svr.windowSwitcher());
        if (params[0] == 0) {
            system.keyRelease(KeyEvent.VK_SHIFT);
        }
    }
}
